//
//  VisitorView.swift
//  FWSinaWeibo
//
//  Created by 范伟 on 16/2/23.
//  Copyright © 2016年 FanWei. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView {
    // MARK: 懒加载私有控件
    private lazy var iconView = UIImageView(fw_imageName: "visitordiscover_feed_image_smallicon")
    ///遮罩视图
    private lazy var maskIconView = UIImageView(fw_imageName: "visitordiscover_feed_mask_smallicon")
    private lazy var homeIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //懒加载时使用闭包
    private lazy var messageLabel = UILabel(fw_text: "", textAlignment: .Center)
    
    ///登录按钮
    lazy var loginButton: UIButton = UIButton(
        fw_title: "登录",
        color: UIColor.orangeColor(),
        backgroundImageName: "common_button_white_disable")
    ///注册按钮
    lazy var registerButton = UIButton(
        fw_title: "注册",
        color: UIColor.orangeColor(),
        backgroundImageName: "common_button_white_disable")
    
    //生命周期函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    // MARK: 禁止storyBoard中的View绑定该类
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: 公共方法
    ///设置访客视图信息
    func setupInformation(imageName: String? = nil, message: String) {
        messageLabel.text = message
        guard let imageName = imageName else {
            startAnimation()
            return
        }
        //非首页需要隐藏`小房子`
        homeIconView.hidden = true
        maskIconView.hidden = true
        iconView.image = UIImage(named: imageName)
    }
    private func startAnimation() {
        let animation = CABasicAnimation()
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //Determines if the animation is removed from the target layer’s animations upon completion.
//        When true, the animation is removed from the target layer’s animations once its active duration has passed. Defaults to true.
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: "transform.rotation")
    }
}

extension VisitorView {
    private func initUI() {
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        // SnapKit自动布局
        // 1> 参照属性一致，可以省略
        // 2> 参照属性不一致，使用 snp_参照属性 的格式
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
//            make.width.equalTo(224)
//            make.height.equalTo(36)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.right.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
    }
}